import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import AppLayout from '@/components/layout/AppLayout.vue'

const pathMatch = {
  path: '/:path(.*)*',
  redirect: '/404'
}


const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    component: AppLayout,
    redirect: '/video',
    children: [
      {
        path: '/discover',
        component: () => import('@/views/discover/Discover.vue'),
        meta: {
          keepAlive: true
        }
      },
      {
        path: '/search/:keyword',
        component: () => import('@/views/search/SearchMusic.vue'),
        meta: {
          keepAlive: false
        }
      },
      {
        path: '/playlist/:type',
        component: () => import('@/views/discover/playListContent/PlayListContent.vue'),
        meta: {
          keepAlive: false
        }
      },
      {
        path: '/video',
        component: () => import('@/views/video/Video.vue'),
        meta: {
          keepAlive: true
        }
      },
      {
        path: '/404',
        name: 'not-found',
        component: () => import('../views/error/404.vue'),
        meta: {
          title: '找不到此页面'
        }
      },
      {
        path: '/video/details/:vid',
        component: () => import('@/views/video/details/VideoDetails.vue'),
        meta: {
          // 隐藏侧边栏
          aside: false,
          player: false
        }
      }
    ]
  },
  {
    path: '/lyric',
    component: () => import('@/components/layout/lyric/Lyric.vue')
  }
]

// 由于使用的是foreach只要找到一个符合的条件就返回了，后续的则无法使用
routes.push({
  ...pathMatch
})
const router = createRouter({
  history: createWebHashHistory(),
  routes: routes
})
// 记录每次路由跳转的地址
// @ts-ignore
router.beforeEach((to, from, next) => {
  window['storage'].set('activePath', to.path)
  next()
})


export default router
